#!/usr/bin/perl -w
# 结合extract与add_user两个脚本来添加学生帐号的用法。
# ./extract_from_excel.pl names.xls | sed -e 's/,/ /g' -e 's/$/ nxy/' | xargs -n 4 -t ./add_user.pl
use strict;
use v5.14;
use utf8;

use Spreadsheet::ParseExcel;

binmode(STDIN,":utf8");
binmode(STDOUT,":utf8");
binmode(STDERR,":utf8");

@ARGV == 1 || die "You must provide input file.";
my $file = $ARGV[0];
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse($file);
$workbook || die $parser->error();
for my $worksheet ($workbook->worksheets()) {
  my $classname = $worksheet->get_name;
  my %hash = ();
  my ($row_min,$row_max) = $worksheet->row_range();
  my ($col_min,$col_max) = $worksheet->col_range();
  for my $row ($row_min..$row_max) {
    for my $col ($col_min..$col_max) {
      my $cell = $worksheet->get_cell($row,$col);
      next unless $cell;
      my $value = $cell->value();
      #抽取学号的正则表达式根据具体excel表中数据的格式来定。
      next unless $value =~ /\d{10,}/;
#      next unless $value =~ /^jx\d+/;
      my $name = $worksheet->get_cell($row,$col+1)->value();
      $hash{$value} = $name;
    }
  }

  while (my($key,$value) = each %hash) {
    say $key,",",$value,",",$classname;
  }
}
